package com.rex.saas.utils;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.symmetric.SM4;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class Sm4CryptoUtil {
    private static final String sm4Key = "1393b136afe77721835bfa7962c3ba28";

    public static void main(String[] args) {
        String testData = "{\\\"VendorCode\\\":\\\"Rexense\\\",\\\"Timestamp\\\":\\\"1763456510\\\",\\\"Seq\\\":\\\"1\\\",\\\"GatewayServer\\\":\\\"00E269A1F048\\\",\\\"GateWayId\\\":\\\"C49894C293FA\\\",\\\"DeviceName\\\":\\\"2c16bdd1a4b6_1_1\\\",\\\"FunctionCode\\\":\\\"WriteDevPropertyList\\\",\\\"Payload\\\":{\\\"PowerSwitch_1\\\":\\\"1\\\",\\\"PowerSwitch_2\\\":\\\"1\\\",\\\"PowerSwitch_3\\\":\\\"1\\\",\\\"PowerSwitch_4\\\":\\\"1\\\"}}";
        String encryptedData = Sm4CryptoUtil.encrypt(testData);
        System.out.println("密⽂⻓度："+encryptedData.length()+"；加密结果: "+ encryptedData);
        String decryptedData = Sm4CryptoUtil.decrypt(encryptedData);
        System.out.println("解密结果: "+decryptedData);
    }

    public static String encrypt(String data) {
        SM4 sm4 = new SM4(HexUtil.decodeHex(sm4Key));
        return sm4.encryptBase64(data);
    }

    public static String decrypt(String data) {
        SM4 sm4 = new SM4(HexUtil.decodeHex(sm4Key));
        return sm4.decryptStr(data);
    }


}